<?php

namespace App\Repositories\Management;

use App\Exceptions\API\APIException;
use App\Exceptions\API\BadRequest;
use App\Models\User;

class ManagementDBRepository implements ManagementRepository
{

    public function getPaginator(ManagementFilter $managementFilter)
    {
        $query = $this->getDBQuery($managementFilter);

        return $query->paginate($managementFilter->getPerPage());
    }

    protected function getDBQuery(ManagementFilter $managementFilter)
    {
        $ManagementQuery = User::query();
        $ManagementQuery->with('department');

        $name = $managementFilter->getName();
        if ($name) {
            $ManagementQuery->where('name','like','%'.$name.'%');
        }

        $departmentName = $managementFilter->getDepartmentName();
        if($departmentName){
            $ManagementQuery->whereHas('department',function ($query) use($departmentName){
                $query->where('name','like','%'.$departmentName.'%');
            });
        }


        return $ManagementQuery;
    }

}
